﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Business;

namespace H2.NET.Web.Controls
{
    public partial class DwzAssessmentAdvanced : TaskControl
    {
        public bool Enable = true;
        public List<H2.NET.Business.t_AssessmentOption> Option = new List<H2.NET.Business.t_AssessmentOption>();
       // public List<t_AssessmentOption> Option = new List<t_AssessmentOption>();
        public bool isReturned
        {
            get
            {
                return ViewState["isReturned"] == null ? false : (bool)ViewState["isReturned"];

            }
            set { ViewState["isReturned"] = value; }
        }
        public string AssessmentID
        {
            get
            {
                return ViewState["AssessmentID"] == null ? "" : (string)ViewState["AssessmentID"];

            }
            set { ViewState["AssessmentID"] = value; }
        }

        #region 事件 Load (页面)
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            isReturned = GetReturnStatus();
            if (!Page.IsPostBack)
            {
                LoadOptions();
                string PreStep = ((object[])TaskAPI["PreStep"])[0].ToString();

                //如果是被退回跳转步骤的情况
                //if (PreStep.Trim().ToUpper() == "")
                if (!isReturned)
                {
                    int Selected = 0;
                    RadOpition.Items.Clear();
                    for (int i = 0; i < Option.Count; i++)
                    {
                        RadOpition.Items.Add(new ListItem(Option[i].RadName, Option[i].RadValue));

                        //取得默认值
                        if (Option[i].IsDefault)
                        {
                            Selected = i;
                        }
                    }

                    //初始化“选择项”选择、备注框对应值
                    if (Option.Count > 0)
                    {
                        RadOpition.Items[Selected].Selected = true;
                    }
                }

                //设置是否只读
                StepLabel.Text = TaskAPI.Step;
                RadOpition.Enabled = Enable;
                Remark.Enabled = Enable;
            }
        }
        private void LoadOptions()
        {
            Option.Clear();
            IList<t_AssessmentOptions> Options = (t_AssessmentOptions._Status == 1 & t_AssessmentOptions._Process == TaskAPI.Process & t_AssessmentOptions._Step == TaskAPI.Step).List<t_AssessmentOptions>();
            foreach (t_AssessmentOptions Opt in Options)
            {
                if (isReturned && Opt.isReturned == "0")
                { }
                else
                {
                    Option.Add(new t_AssessmentOption(Opt.OptionName, Opt.OptionValue, false));
                }
            }
            
        }
        #endregion

        #region 事件 变换选择项
        protected void RadOpition_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                switch (RadOpition.SelectedValue)
                {
                    case "2":
                        SetVisible(true, BackTo_L, BackTo);
                        BackTo.Items.Clear();
                        IList<vw_AssessmentReturn> Options = (vw_AssessmentReturn._Process == TaskAPI.Process & vw_AssessmentReturn._Step == TaskAPI.Step & vw_AssessmentReturn._Incident == TaskAPI.Incident).List<vw_AssessmentReturn>();
                        foreach (vw_AssessmentReturn Opt in Options)
                        {
                            if (isReturned)
                            { }
                            else
                            {
                                BackTo.Items.Add(new ListItem(Opt.ReturnToStep, Opt.ReturnToStep));
                            }
                        }
                        break;
                    default:
                        BackTo.Items.Clear();
                        SetVisible(false, BackTo_L, BackTo);
                        break;
                }
            }
            catch (Exception)
            {
            }
        }
        #endregion

        #region 方法 提取Assessment
        public t_Assessment Assessment
        {
            get
            {
                t_Assessment ass = new t_Assessment();
                ass.Process = TaskAPI.Process;
                ass.Incident = TaskAPI.Incident;
                ass.Step = TaskAPI.Step;
                ass.UserName = TaskAPI.UserName;
                ass.UserFullName = TaskAPI.UserFullName;
                ass.AssignedToUserName = UserAPI.UserName;
                ass.AssignedToUserFullName = UserAPI.UserFullName;
                ass.Department = UserAPI.Department;
                ass.StartTime = TaskAPI.StartTime;
                ass.Time = System.DateTime.Now;
                ass.Opinion = RadOpition.SelectedValue;
                ass.OpinionText = RadOpition.SelectedItem == null ? "" : RadOpition.SelectedItem.Text;
                ass.Opinion2 = "";
                ass.OpinionText2 = "";
                ass.Status = 1;
                ass.ReturnTo = RadOpition.SelectedValue == "1" ? "" : BackTo.SelectedValue;
                foreach (ListItem li in RadOpition.Items)
                {
                    ass.Opinion2 += li.Value + "~";
                    ass.OpinionText2 += li.Text + "~";
                }
                ass.Remark = Remark.Text;
                AssessmentID = ass.ID;
                return ass;
            }
            set
            {
                AssessmentID = value.ID;
            }
        }
        #endregion

        #region 方法 跳转步骤
        public string JumpStep
        {
            get
            {
                //string[] isJumpValue = ((object[])TaskAPI["IsJumpValue"])[0].ToString().Split(',');
                bool flg = false;

                //foreach (string jv in isJumpValue)
                //{
                //    if (RadOpition.SelectedValue == jv)
                //    {
                //        flg = true;
                //    }
                //}
                flg = RadOpition.SelectedValue == "2";
                return flg ? BackTo.SelectedItem != null ? BackTo.SelectedItem.Text : "" : "";
            }
        }
        public string JumpToID
        {
            get
            {
                string[] isJumpValue = ((object[])TaskAPI["IsJumpValue"])[0].ToString().Split(',');
                bool flg = false;

                foreach (string jv in isJumpValue)
                {
                    if (RadOpition.SelectedValue == jv)
                    {
                        flg = true;
                    }
                }
                return flg ? BackTo.SelectedValue : "";
            }
        }
        public void SubmitTo(bool isMulti)
        {
            if (isReturned)
            {
                IList<t_Assessment> tass = (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == TaskAPI.Incident & t_Assessment.returnTo == TaskAPI.Step).List<t_Assessment>();
                foreach (t_Assessment tas in tass)
                {
                    if (!isMulti) { SetStepUserName(tas.Step, tas.UserName); }
                    TaskAPI.DirectActive(tas.Step);
                }
                ClearReturnInfo();
                TaskAPI["Step"] = "isReturned";
                TaskAPI["PreStep"] = "isReturned";
            }
            TaskAPI["AR_" + TaskAPI.Step.Replace(" ", "_")] = Assessment.Opinion;
            TaskAPI["EMAIL_" + TaskAPI.Step.Replace(" ", "_")] = UserAPI.Mail;
        }
        public void SetAsReturn(bool r)
        {
            if (r)
            {
                TaskAPI["Step"] = "isReturned";
                TaskAPI["PreStep"] = "isReturned";
            }
            else
            {
                TaskAPI["Step"] = "";
                TaskAPI["PreStep"] = "";
            }
        }
        public bool GetReturnStatus()
        {
            IList<t_Assessment> ass_s = (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == TaskAPI.Incident & t_Assessment.returnTo == TaskAPI.Step).List<t_Assessment>();
            if (ass_s.Count <= 0)
            {
                TaskAPI["Step"] = "";
                TaskAPI["PreStep"] = "";
                return false;
            }
            else
            {
                TaskAPI["Step"] = "isReturned";
                TaskAPI["PreStep"] = "isReturned";
                return true;
            }
        }
        public void ClearReturnInfo()
        {
            (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == TaskAPI.Incident & t_Assessment.returnTo == TaskAPI.Step).Edit<t_Assessment>(p =>
            {
                p.ReturnTo = "";
            }
            );
        }
        public void SetStepUserName(string Step, string Username)
        {
            if (TaskAPI["R_" + Step.Replace(" ", "_")] != null)
            {
                object[] obj = (object[])(TaskAPI["R_" + Step.Replace(" ", "_")]);
                for (int i = 0; i < obj.Length; i++)
                {
                    obj[i] = null;
                }
                obj[0] = Username;
                TaskAPI["R_" + Step.Replace(" ", "_")] = obj;
            }
        }
        #endregion
    }
}